package deng.qiya.util;





import it.sauronsoftware.base64.Base64;

import java.io.IOException;





import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.DefaultHttpClient;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Example how to use unbuffered chunk-encoded POST request.
 */
public class HttpPostUtil {

	
	private static final Logger logger = LoggerFactory.getLogger(HttpPostUtil.class);
	
    public static String postByStream(String url, String str)  {

    	//System.out.println(url);
    	//System.out.println(str);
        HttpClient httpclient = new DefaultHttpClient();
        try {
            HttpPost httppost = new HttpPost(url);

            InputStreamEntity reqEntity = new InputStreamEntity(IOUtils.toInputStream(str, "UTF-8"), -1);
            reqEntity.setContentType("binary/octet-stream");
            reqEntity.setContentEncoding("UTF-8");
            httppost.setEntity(reqEntity);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity resEntity = response.getEntity();

            if (resEntity != null && response != null && response.getStatusLine().getStatusCode() == 200) {
                String result = IOUtils.toString(resEntity.getContent(),"UTF-8");
                return result;
            }
            //EntityUtils.consume(resEntity);
        } catch (IOException e) {
			e.printStackTrace();
		} finally {
            httpclient.getConnectionManager().shutdown();
        }
        return null;
    }
    
    
    public static String postBy64Stream(String url, String str)  {
    	
    	str = Base64.encode(str,"UTF-8");
    	
    	
        HttpClient httpclient = new DefaultHttpClient();
        try {
            HttpPost httppost = new HttpPost(url);

            InputStreamEntity reqEntity = new InputStreamEntity(IOUtils.toInputStream(str, "UTF-8"), -1);
            reqEntity.setContentType("binary/octet-stream");
            reqEntity.setContentEncoding("UTF-8");
            
            //Base64.encode();
            httppost.setEntity(reqEntity);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity resEntity = response.getEntity();

            if (resEntity != null && response != null && response.getStatusLine().getStatusCode() == 200) {
                String result = IOUtils.toString(resEntity.getContent(),"UTF-8");
                return result;
            }
            //EntityUtils.consume(resEntity);
        } catch (IOException e) {
			e.printStackTrace();
		} finally {
            httpclient.getConnectionManager().shutdown();
        }
        return null;
    }
    
    public static String GetByStream(String url)
    {
    	
    	 HttpClient httpclient = new DefaultHttpClient();
         try {
            // HttpPost httppost = new HttpPost(url);
             HttpGet httpget=new HttpGet(url);
            // InputStreamEntity reqEntity = new InputStreamEntity(IOUtils.toInputStream(str, "UTF-8"), -1);
            // reqEntity.setContentType("binary/octet-stream");
           //  reqEntity.setContentEncoding("UTF-8");
            // httppost.setEntity(reqEntity);
             HttpResponse response = httpclient.execute(httpget);
             HttpEntity resEntity = response.getEntity();

             if (resEntity != null && response != null && response.getStatusLine().getStatusCode() == 200) {
                 String result = IOUtils.toString(resEntity.getContent(),"UTF-8");
                 return result;
             }
             //EntityUtils.consume(resEntity);
         } catch (IOException e) {
 			e.printStackTrace();
 		} finally {
             httpclient.getConnectionManager().shutdown();
         }
         return null;
    }

}

